﻿using System;
using System.Data;
using System.Linq;
using System.Web.Mvc;
using WarOfDev.EF;
using WarOfDev.Web.Models.MappingToViewModel;
using WarOfDev.Web.ViewModels;
using WarOfDev.Web.ViewModels.MappingToModel;

namespace WarOfDev.Web.Controllers
{
    public class PermissionController : Controller
    {
        private WarDevEntities db = new WarDevEntities();

        //
        // GET: /Permission/

        public ActionResult Index()
        {
            var permissions = db.Permissions.Include("Module").AsEnumerable().OrderBy(p => p.ModuleId).ThenBy(p => p.PermissionValue);
            return View(permissions.Select(p => p.ToViewModel()).ToList());
        }

        //
        // GET: /Permission/Details/5

        public ActionResult Details(string id = null)
        {
            Permission permission = db.Permissions.Single(p => p.Id == id);
            if (permission == null)
            {
                return HttpNotFound();
            }
            return View(permission);
        }

        //
        // GET: /Permission/Create

        public ActionResult Create()
        {
            ViewBag.ModuleId = new SelectList(db.Modules.OrderBy(p => p.ParentId), "Id", "ModuleName");
            return View();
        }

        //
        // POST: /Permission/Create

        [HttpPost]
        public ActionResult Create(PermissionViewModel permission)
        {
            if (ModelState.IsValid)
            {
                permission.Id = Guid.NewGuid().ToString();
                permission.CreatedOn = DateTime.Now;
                permission.ModifiedOn = DateTime.Now;

                db.Permissions.AddObject(permission.ToModel());
                db.SaveChanges();
                return RedirectToAction("Index");
            }

            ViewBag.ModuleId = new SelectList(db.Modules.OrderBy(p => p.ParentId), "Id", "ModuleName", permission.ModuleId);
            return View(permission);
        }

        //
        // GET: /Permission/Edit/5

        public ActionResult Edit(string id = null)
        {
            Permission permission = db.Permissions.Single(p => p.Id == id);
            if (permission == null)
            {
                return HttpNotFound();
            }
            ViewBag.ModuleId = new SelectList(db.Modules, "Id", "ModuleName", permission.ModuleId);
            return View(permission);
        }

        //
        // POST: /Permission/Edit/5

        [HttpPost]
        public ActionResult Edit(Permission permission)
        {
            if (ModelState.IsValid)
            {
                db.Permissions.Attach(permission);
                db.ObjectStateManager.ChangeObjectState(permission, EntityState.Modified);
                db.SaveChanges();
                return RedirectToAction("Index");
            }
            ViewBag.ModuleId = new SelectList(db.Modules.OrderBy(p => p.ParentId), "Id", "ModuleName", permission.ModuleId);
            return View(permission);
        }

        //
        // GET: /Permission/Delete/5

        public ActionResult Delete(string id = null)
        {
            Permission permission = db.Permissions.Single(p => p.Id == id);
            if (permission == null)
            {
                return HttpNotFound();
            }
            return View(permission);
        }

        //
        // POST: /Permission/Delete/5

        [HttpPost, ActionName("Delete")]
        public ActionResult DeleteConfirmed(string id)
        {
            Permission permission = db.Permissions.Single(p => p.Id == id);
            db.Permissions.DeleteObject(permission);
            db.SaveChanges();
            return RedirectToAction("Index");
        }

        protected override void Dispose(bool disposing)
        {
            db.Dispose();
            base.Dispose(disposing);
        }
    }
}